
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppEventCate;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppEventCateRepository;
import me.zhengjie.service.AppEventCateService;
import me.zhengjie.service.dto.AppEventCateDto;
import me.zhengjie.service.dto.AppEventCateQueryCriteria;
import me.zhengjie.service.mapstruct.AppEventCateMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-07-03
**/
@Service
@RequiredArgsConstructor
public class AppEventCateServiceImpl implements AppEventCateService {

    private final AppEventCateRepository appEventCateRepository;
    private final AppEventCateMapper appEventCateMapper;

    @Override
    public Map<String,Object> queryAll(AppEventCateQueryCriteria criteria, Pageable pageable){
        Page<AppEventCate> page = appEventCateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appEventCateMapper::toDto));
    }

    @Override
    public Map<String,Object> queryAllApi(AppEventCateQueryCriteria criteria, Pageable pageable){
        Page<AppEventCate> page = appEventCateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPages(page.map(appEventCateMapper::toDto),"");
    }

    @Override
    public List<AppEventCateDto> queryAll(AppEventCateQueryCriteria criteria){
        return appEventCateMapper.toDto(appEventCateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public List<AppEventCateDto> queryAllApi(AppEventCateQueryCriteria criteria){
        return appEventCateMapper.toDto(appEventCateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AppEventCateDto findById(Integer cateId) {
        AppEventCate appEventCate = appEventCateRepository.findById(cateId).orElseGet(AppEventCate::new);
        ValidationUtil.isNull(appEventCate.getCateId(),"AppEventCate","cateId",cateId);
        return appEventCateMapper.toDto(appEventCate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppEventCateDto create(AppEventCate resources) {
        return appEventCateMapper.toDto(appEventCateRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppEventCate resources) {
        AppEventCate appEventCate = appEventCateRepository.findById(resources.getCateId()).orElseGet(AppEventCate::new);
        ValidationUtil.isNull( appEventCate.getCateId(),"AppEventCate","id",resources.getCateId());
        appEventCate.copy(resources);
        appEventCateRepository.save(appEventCate);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer cateId : ids) {
            appEventCateRepository.deleteById(cateId);
        }
    }

    @Override
    public void download(List<AppEventCateDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppEventCateDto appEventCate : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", appEventCate.getTitle());
            map.put("图片地址", appEventCate.getImg());
            map.put("1普通事件，2紧急事件", appEventCate.getType());
            map.put("排序（越小越前）", appEventCate.getOrdid());
            map.put("状态", appEventCate.getStatus());
            map.put("创建日期", appEventCate.getCreateTime());
            map.put("更新日期", appEventCate.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}